package com.neuedu.cockpit.controller;

import com.neuedu.cockpit.domain.CarMileage;
import com.neuedu.cockpit.service.ICarMileageService;
import com.neuedu.cockpit.service.IHmCarInfoService;
import com.neuedu.common.core.controller.BaseController;
import com.neuedu.common.core.domain.AjaxResult;
import com.neuedu.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 车辆状态Controller
 *
 * @author neuedu
 * @date 2024-07-08
 */
@RestController
@RequestMapping("/train/mileage")
public class CarMileageController extends BaseController {
    @Autowired
    private ICarMileageService carMileageService;

    @Autowired
    private IHmCarInfoService carInfoService;

    /**
     * 查询车辆状态列表
     */
//    @PreAuthorize("@ss.hasPermi('train:mileage:list')")
    @GetMapping("/list")
    public TableDataInfo list(CarMileage carMileage) {
        startPage();
        List<CarMileage> list = carMileageService.selectCarMileageList(carMileage);
        return getDataTable(list);
    }

    /**
     * 新增车辆状态
     */
//    @PreAuthorize("@ss.hasPermi('train:mileage:add')")
    @PostMapping
    public AjaxResult add(@RequestBody CarMileage carMileage) {
        return toAjax(carMileageService.insertCarMileage(carMileage));
    }

    @GetMapping("/stat")
    public AjaxResult stat() {
        HashMap result = new HashMap();
        List<CarMileage> list = carMileageService.stat();
        if (list.size() > 0) {
            long carId = 0;
            List<CarMileage> subList = null;
            for (CarMileage carMileage : list) {
                // 新车
                if (carId != carMileage.getCarId()) {
                    // 新车
                    if (subList != null) {
                        String carName = carInfoService.selectHmCarInfoById(carId).getCarName();
                        result.put(carName, subList);
                    }
                    carId = carMileage.getCarId();
                    subList = new ArrayList<>();
                    subList.add(carMileage);
                } else {
                    subList.add(carMileage);
                }
            }
            String carName = carInfoService.selectHmCarInfoById(carId).getCarName();
            result.put(carName, subList);
        }
        return AjaxResult.success(result);
    }

}
